package com.ruoyi.functiontest.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.functiontest.domain.RoadSectionDistanceTest;
import com.ruoyi.functiontest.service.IRoadSectionDistanceTestService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 主线站点间里程实测数据测试Controller
 * 
 * @author ruoyi
 * @date 2025-09-25
 */
@RestController
@RequestMapping("/functiontest/RoadSectionDistanceTest")
public class RoadSectionDistanceTestController extends BaseController
{
    @Autowired
    private IRoadSectionDistanceTestService roadSectionDistanceTestService;

    /**
     * 查询主线站点间里程实测数据测试列表
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:list')")
    @GetMapping("/list")
    public AjaxResult list(RoadSectionDistanceTest roadSectionDistanceTest)
    {
        List<RoadSectionDistanceTest> list = roadSectionDistanceTestService.selectRoadSectionDistanceTestList(roadSectionDistanceTest);
        
        // 判断是否有搜索条件，如果有则构建树形结构返回
        boolean hasSearchCondition = roadSectionDistanceTest.getRoadName() != null && !roadSectionDistanceTest.getRoadName().isEmpty() ||
                                   roadSectionDistanceTest.getNodeName() != null && !roadSectionDistanceTest.getNodeName().isEmpty();
        
        if (hasSearchCondition) {
            // 有搜索条件时，构建树形结构返回，包含匹配节点及其所有子节点
            List<RoadSectionDistanceTest> treeList = buildTree(list);
            return success(treeList);
        } else {
            // 无搜索条件时，返回扁平列表
            return success(list);
        }
    }

    /**
     * 导出主线站点间里程实测数据测试列表
     * 按照树形结构的层次顺序导出所有子树数据
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:export')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RoadSectionDistanceTest roadSectionDistanceTest)
    {
        // 获取所有数据
        List<RoadSectionDistanceTest> list = roadSectionDistanceTestService.selectRoadSectionDistanceTestList(roadSectionDistanceTest);
        
        // 构建树形结构
        List<RoadSectionDistanceTest> treeList = buildTree(list);
        
        // 将树形结构按照层次顺序展平为列表
        List<RoadSectionDistanceTest> flattenedList = new ArrayList<>();
        flattenTree(treeList, flattenedList);
        
        ExcelUtil<RoadSectionDistanceTest> util = new ExcelUtil<RoadSectionDistanceTest>(RoadSectionDistanceTest.class);
        util.exportExcel(response, flattenedList, "主线站点间里程实测数据测试数据");
    }

    /**
     * 获取主线站点间里程实测数据测试详细信息
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(roadSectionDistanceTestService.selectRoadSectionDistanceTestById(id));
    }

    /**
     * 新增主线站点间里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:add')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return toAjax(roadSectionDistanceTestService.insertRoadSectionDistanceTest(roadSectionDistanceTest));
    }

    /**
     * 修改主线站点间里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:edit')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RoadSectionDistanceTest roadSectionDistanceTest)
    {
        return toAjax(roadSectionDistanceTestService.updateRoadSectionDistanceTest(roadSectionDistanceTest));
    }

    /**
     * 删除主线站点间里程实测数据测试
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:remove')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        // 检查是否可以删除节点（父节点必须在子节点删除完后才能删除）
        for (Long id : ids) {
            if (roadSectionDistanceTestService.hasChildById(id)) {
                return error("节点存在子节点，请先删除子节点后再删除父节点");
            }
        }
        
        return toAjax(roadSectionDistanceTestService.deleteRoadSectionDistanceTestByIds(ids));
    }
    
    /**
     * 导入主线站点间里程实测数据测试数据
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:import')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RoadSectionDistanceTest> util = new ExcelUtil<RoadSectionDistanceTest>(RoadSectionDistanceTest.class);
        List<RoadSectionDistanceTest> importList = util.importExcel(file.getInputStream());
        
        String message = roadSectionDistanceTestService.importRoadSectionDistanceTest(importList, updateSupport);
        return success(message);
    }

    /**
     * 导入模板下载
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:import')")
    @GetMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        List<RoadSectionDistanceTest> list = new ArrayList<RoadSectionDistanceTest>();
        
        // 添加示例数据 - 六安路段
        RoadSectionDistanceTest data1 = new RoadSectionDistanceTest();
        data1.setId(1L);
        data1.setParentId(0L);
        data1.setRoadName("六安");
        data1.setNodeName("");
        data1.setLeftLength(new java.math.BigDecimal("10834.2200"));
        data1.setRightLength(new java.math.BigDecimal("10830.9700"));
        data1.setAvgLength(new java.math.BigDecimal("10832.6000"));
        data1.setTotalLeftLength(new java.math.BigDecimal("0"));
        data1.setTotalRightLength(new java.math.BigDecimal("0"));
        data1.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data1);
        
        RoadSectionDistanceTest data2 = new RoadSectionDistanceTest();
        data2.setId(2L);
        data2.setParentId(1L);
        data2.setRoadName("");
        data2.setNodeName("主线起点（同云枢纽互通匝道汇聚点）/月亮河互通");
        data2.setLeftLength(new java.math.BigDecimal("12191.6000"));
        data2.setRightLength(new java.math.BigDecimal("12219.6400"));
        data2.setAvgLength(new java.math.BigDecimal("12205.6200"));
        data2.setTotalLeftLength(new java.math.BigDecimal("0"));
        data2.setTotalRightLength(new java.math.BigDecimal("0"));
        data2.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data2);
        
        RoadSectionDistanceTest data3 = new RoadSectionDistanceTest();
        data3.setId(3L);
        data3.setParentId(1L);
        data3.setRoadName("");
        data3.setNodeName("月亮河互通/郎岱互通");
        data3.setLeftLength(new java.math.BigDecimal("5275.2100"));
        data3.setRightLength(new java.math.BigDecimal("5275.6750"));
        data3.setAvgLength(new java.math.BigDecimal("5275.4425"));
        data3.setTotalLeftLength(new java.math.BigDecimal("0"));
        data3.setTotalRightLength(new java.math.BigDecimal("0"));
        data3.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data3);
        
        RoadSectionDistanceTest data6 = new RoadSectionDistanceTest();
        data6.setId(6L);
        data6.setParentId(1L);
        data6.setRoadName("");
        data6.setNodeName("郎岱互通/关岭北互通");
        data6.setLeftLength(new java.math.BigDecimal("11612.1250"));
        data6.setRightLength(new java.math.BigDecimal("11612.3450"));
        data6.setAvgLength(new java.math.BigDecimal("11612.2350"));
        data6.setTotalLeftLength(new java.math.BigDecimal("0"));
        data6.setTotalRightLength(new java.math.BigDecimal("0"));
        data6.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data6);
        
        RoadSectionDistanceTest data7 = new RoadSectionDistanceTest();
        data7.setId(7L);
        data7.setParentId(1L);
        data7.setRoadName("");
        data7.setNodeName("关岭北互通/顶云枢纽互通");
        data7.setLeftLength(new java.math.BigDecimal("7278.8400"));
        data7.setRightLength(new java.math.BigDecimal("7264.9450"));
        data7.setAvgLength(new java.math.BigDecimal("7271.8925"));
        data7.setTotalLeftLength(new java.math.BigDecimal("0"));
        data7.setTotalRightLength(new java.math.BigDecimal("0"));
        data7.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data7);
        
        RoadSectionDistanceTest data9 = new RoadSectionDistanceTest();
        data9.setId(9L);
        data9.setParentId(1L);
        data9.setRoadName("");
        data9.setNodeName("顶云枢纽互通/关岭南互通");
        data9.setLeftLength(new java.math.BigDecimal("5957.1850"));
        data9.setRightLength(new java.math.BigDecimal("5955.2600"));
        data9.setAvgLength(new java.math.BigDecimal("5956.2225"));
        data9.setTotalLeftLength(new java.math.BigDecimal("0"));
        data9.setTotalRightLength(new java.math.BigDecimal("0"));
        data9.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data9);
        
        RoadSectionDistanceTest data11 = new RoadSectionDistanceTest();
        data11.setId(11L);
        data11.setParentId(1L);
        data11.setRoadName("");
        data11.setNodeName("关岭南互通/花江互通");
        data11.setLeftLength(new java.math.BigDecimal("13354.4500"));
        data11.setRightLength(new java.math.BigDecimal("13329.5800"));
        data11.setAvgLength(new java.math.BigDecimal("13342.0150"));
        data11.setTotalLeftLength(new java.math.BigDecimal("0"));
        data11.setTotalRightLength(new java.math.BigDecimal("0"));
        data11.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data11);
        
        RoadSectionDistanceTest data12 = new RoadSectionDistanceTest();
        data12.setId(12L);
        data12.setParentId(1L);
        data12.setRoadName("");
        data12.setNodeName("花江互通/云渡互通");
        data12.setLeftLength(new java.math.BigDecimal("0.0000"));
        data12.setRightLength(new java.math.BigDecimal("0.0000"));
        data12.setAvgLength(null);
        data12.setTotalLeftLength(new java.math.BigDecimal("0"));
        data12.setTotalRightLength(new java.math.BigDecimal("0"));
        data12.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data12);
        
        // 添加示例数据 - 纳晴路段
        RoadSectionDistanceTest data13 = new RoadSectionDistanceTest();
        data13.setId(13L);
        data13.setParentId(0L);
        data13.setRoadName("纳晴");
        data13.setNodeName("");
        data13.setLeftLength(new java.math.BigDecimal("4739.1900"));
        data13.setRightLength(new java.math.BigDecimal("4732.5300"));
        data13.setAvgLength(new java.math.BigDecimal("4735.8600"));
        data13.setTotalLeftLength(new java.math.BigDecimal("0"));
        data13.setTotalRightLength(new java.math.BigDecimal("0"));
        data13.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data13);
        
        RoadSectionDistanceTest data14 = new RoadSectionDistanceTest();
        data14.setId(14L);
        data14.setParentId(13L);
        data14.setRoadName("");
        data14.setNodeName("云渡互通/小花江互通");
        data14.setLeftLength(new java.math.BigDecimal("16697.6400"));
        data14.setRightLength(new java.math.BigDecimal("16667.5850"));
        data14.setAvgLength(new java.math.BigDecimal("16682.6125"));
        data14.setTotalLeftLength(new java.math.BigDecimal("0"));
        data14.setTotalRightLength(new java.math.BigDecimal("0"));
        data14.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data14);
        
        RoadSectionDistanceTest data15 = new RoadSectionDistanceTest();
        data15.setId(15L);
        data15.setParentId(13L);
        data15.setRoadName("");
        data15.setNodeName("小花江互通/北盘江互通");
        data15.setLeftLength(new java.math.BigDecimal("7543.5950"));
        data15.setRightLength(new java.math.BigDecimal("7536.4050"));
        data15.setAvgLength(new java.math.BigDecimal("7540.0000"));
        data15.setTotalLeftLength(new java.math.BigDecimal("0"));
        data15.setTotalRightLength(new java.math.BigDecimal("0"));
        data15.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data15);
        
        RoadSectionDistanceTest data16 = new RoadSectionDistanceTest();
        data16.setId(16L);
        data16.setParentId(13L);
        data16.setRoadName("");
        data16.setNodeName("北盘江互通/双乳峰互通");
        data16.setLeftLength(new java.math.BigDecimal("12951.6050"));
        data16.setRightLength(new java.math.BigDecimal("12947.3650"));
        data16.setAvgLength(new java.math.BigDecimal("12949.4850"));
        data16.setTotalLeftLength(new java.math.BigDecimal("0"));
        data16.setTotalRightLength(new java.math.BigDecimal("0"));
        data16.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data16);
        
        RoadSectionDistanceTest data17 = new RoadSectionDistanceTest();
        data17.setId(17L);
        data17.setParentId(13L);
        data17.setRoadName("");
        data17.setNodeName("双乳峰互通/贞丰枢纽互通");
        data17.setLeftLength(new java.math.BigDecimal("6348.5350"));
        data17.setRightLength(new java.math.BigDecimal("6356.2200"));
        data17.setAvgLength(new java.math.BigDecimal("6352.3775"));
        data17.setTotalLeftLength(new java.math.BigDecimal("0"));
        data17.setTotalRightLength(new java.math.BigDecimal("0"));
        data17.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data17);
        
        RoadSectionDistanceTest data18 = new RoadSectionDistanceTest();
        data18.setId(18L);
        data18.setParentId(13L);
        data18.setRoadName("");
        data18.setNodeName("贞丰枢纽互通/笃山互通");
        data18.setLeftLength(new java.math.BigDecimal("20824.8550"));
        data18.setRightLength(new java.math.BigDecimal("20796.9350"));
        data18.setAvgLength(new java.math.BigDecimal("20810.8950"));
        data18.setTotalLeftLength(new java.math.BigDecimal("0"));
        data18.setTotalRightLength(new java.math.BigDecimal("0"));
        data18.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data18);
        
        RoadSectionDistanceTest data19 = new RoadSectionDistanceTest();
        data19.setId(19L);
        data19.setParentId(13L);
        data19.setRoadName("");
        data19.setNodeName("笃山互通/安龙北互通");
        data19.setLeftLength(new java.math.BigDecimal("14865.2250"));
        data19.setRightLength(new java.math.BigDecimal("14884.0950"));
        data19.setAvgLength(new java.math.BigDecimal("14874.6600"));
        data19.setTotalLeftLength(new java.math.BigDecimal("0"));
        data19.setTotalRightLength(new java.math.BigDecimal("0"));
        data19.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data19);
        
        RoadSectionDistanceTest data20 = new RoadSectionDistanceTest();
        data20.setId(20L);
        data20.setParentId(13L);
        data20.setRoadName("");
        data20.setNodeName("安龙北互通/主线终点（木咱枢纽互通匝道汇聚点）");
        data20.setLeftLength(new java.math.BigDecimal("16778.2750"));
        data20.setRightLength(new java.math.BigDecimal("16787.4900"));
        data20.setAvgLength(new java.math.BigDecimal("16782.8825"));
        data20.setTotalLeftLength(new java.math.BigDecimal("0"));
        data20.setTotalRightLength(new java.math.BigDecimal("0"));
        data20.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data20);
        
        RoadSectionDistanceTest data21 = new RoadSectionDistanceTest();
        data21.setId(21L);
        data21.setParentId(13L);
        data21.setRoadName("");
        data21.setNodeName("测量起点 （寨乐枢纽互通起点）/ETC门架");
        data21.setLeftLength(new java.math.BigDecimal("10628.1200"));
        data21.setRightLength(new java.math.BigDecimal("10521.8000"));
        data21.setAvgLength(new java.math.BigDecimal("10574.9600"));
        data21.setTotalLeftLength(new java.math.BigDecimal("0"));
        data21.setTotalRightLength(new java.math.BigDecimal("0"));
        data21.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data21);
        
        RoadSectionDistanceTest data22 = new RoadSectionDistanceTest();
        data22.setId(22L);
        data22.setParentId(13L);
        data22.setRoadName("");
        data22.setNodeName("ETC门架/纳雍西收费站");
        data22.setLeftLength(new java.math.BigDecimal("1698.0600"));
        data22.setRightLength(new java.math.BigDecimal("1822.0400"));
        data22.setAvgLength(new java.math.BigDecimal("1760.0500"));
        data22.setTotalLeftLength(new java.math.BigDecimal("0"));
        data22.setTotalRightLength(new java.math.BigDecimal("0"));
        data22.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data22);
        
        RoadSectionDistanceTest data23 = new RoadSectionDistanceTest();
        data23.setId(23L);
        data23.setParentId(13L);
        data23.setRoadName("");
        data23.setNodeName("纳雍西收费站/ETC门架");
        data23.setLeftLength(new java.math.BigDecimal("800.3600"));
        data23.setRightLength(new java.math.BigDecimal("675.0200"));
        data23.setAvgLength(new java.math.BigDecimal("737.6900"));
        data23.setTotalLeftLength(new java.math.BigDecimal("0"));
        data23.setTotalRightLength(new java.math.BigDecimal("0"));
        data23.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data23);
        
        RoadSectionDistanceTest data24 = new RoadSectionDistanceTest();
        data24.setId(24L);
        data24.setParentId(13L);
        data24.setRoadName("");
        data24.setNodeName("ETC门架/张维收费站");
        data24.setLeftLength(new java.math.BigDecimal("9447.5900"));
        data24.setRightLength(new java.math.BigDecimal("9551.6800"));
        data24.setAvgLength(new java.math.BigDecimal("9499.6350"));
        data24.setTotalLeftLength(new java.math.BigDecimal("0"));
        data24.setTotalRightLength(new java.math.BigDecimal("0"));
        data24.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data24);
        
        RoadSectionDistanceTest data25 = new RoadSectionDistanceTest();
        data25.setId(25L);
        data25.setParentId(13L);
        data25.setRoadName("");
        data25.setNodeName("张维收费站/ETC门架");
        data25.setLeftLength(new java.math.BigDecimal("2233.0800"));
        data25.setRightLength(new java.math.BigDecimal("2050.8700"));
        data25.setAvgLength(new java.math.BigDecimal("2141.9750"));
        data25.setTotalLeftLength(new java.math.BigDecimal("0"));
        data25.setTotalRightLength(new java.math.BigDecimal("0"));
        data25.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data25);
        
        RoadSectionDistanceTest data26 = new RoadSectionDistanceTest();
        data26.setId(26L);
        data26.setParentId(13L);
        data26.setRoadName("");
        data26.setNodeName("ETC门架/织金鸡场收费站");
        data26.setLeftLength(new java.math.BigDecimal("16216.2600"));
        data26.setRightLength(new java.math.BigDecimal("16396.3800"));
        data26.setAvgLength(new java.math.BigDecimal("16306.3200"));
        data26.setTotalLeftLength(new java.math.BigDecimal("0"));
        data26.setTotalRightLength(new java.math.BigDecimal("0"));
        data26.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data26);
        
        RoadSectionDistanceTest data27 = new RoadSectionDistanceTest();
        data27.setId(27L);
        data27.setParentId(13L);
        data27.setRoadName("");
        data27.setNodeName("织金鸡场收费站/ETC门架");
        data27.setLeftLength(new java.math.BigDecimal("2175.7400"));
        data27.setRightLength(new java.math.BigDecimal("2072.9200"));
        data27.setAvgLength(new java.math.BigDecimal("2124.3300"));
        data27.setTotalLeftLength(new java.math.BigDecimal("0"));
        data27.setTotalRightLength(new java.math.BigDecimal("0"));
        data27.setTotalAvgLength(new java.math.BigDecimal("0"));
        list.add(data27);
        
        ExcelUtil<RoadSectionDistanceTest> util = new ExcelUtil<RoadSectionDistanceTest>(RoadSectionDistanceTest.class);
        util.exportExcel(response, list, "主线站点间里程实测数据测试导入模板");
    }
    
    /**
     * 实测单位批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:fieldAudit')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/fieldAudit/batch")
    public AjaxResult fieldAuditBatch()
    {
        try {
            // 批量更新所有记录的review1字段为0
            int updatedCount = roadSectionDistanceTestService.updateReview1ToZero();
            System.out.println("实测单位批量审核完成，更新记录数：" + updatedCount);
            return success("实测单位批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("实测单位批量审核失败：" + e.getMessage());
            return error("实测单位批量审核失败：" + e.getMessage());
        }
    }
    
    /**
     * 业主批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:ownerAudit')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/ownerAudit/batch")
    public AjaxResult ownerAuditBatch(@RequestBody Map<String, Object> params)
    {
        try {
            Long deptId = params.get("deptId") != null ? Long.valueOf(params.get("deptId").toString()) : null;
            Long roleId = params.get("roleId") != null ? Long.valueOf(params.get("roleId").toString()) : null;
            
            System.out.println("业主批量审核 - 审核所有数据，deptId: " + deptId + ", roleId: " + roleId);
            
            // 1. 校验所有数据的review1字段是否都为0
            boolean allReview1Zero = roadSectionDistanceTestService.checkAllReview1IsZero();
            if (!allReview1Zero) {
                return error("业主批量审核失败：存在未通过实测单位审核的数据，请先完成实测单位审核");
            }
            
            // 2. 构建查询条件
            RoadSectionDistanceTest queryCondition = new RoadSectionDistanceTest();
            queryCondition.setDeptId(deptId);
            queryCondition.setRoleId(roleId);
            
            // 3. 从params中提取查询条件
            if (params.get("roadName") != null) {
                queryCondition.setRoadName(params.get("roadName").toString());
            }
            if (params.get("nodeName") != null) {
                queryCondition.setNodeName(params.get("nodeName").toString());
            }
            
            // 4. 根据条件批量更新业主审核状态
            int updatedCount = roadSectionDistanceTestService.updateReview2ToZeroByCondition(queryCondition);
            
            System.out.println("业主批量审核完成，更新记录数：" + updatedCount);
            return success("业主批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("业主批量审核失败：" + e.getMessage());
            return error("业主批量审核失败：" + e.getMessage());
        }
    }
    
    /**
     * 中心批量审核
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:centerAudit')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/centerAudit/batch")
    public AjaxResult centerAuditBatch()
    {
        try {
            System.out.println("中心批量审核 - 审核所有数据");
            
            // 1. 校验所有数据的review1和review2字段是否都不为空且都为0
            boolean allReview1AndReview2Zero = roadSectionDistanceTestService.checkAllReview1AndReview2AreZero();
            if (!allReview1AndReview2Zero) {
                return error("中心批量审核失败：存在未通过实测单位审核或业主审核的数据，请先完成前序审核");
            }
            
            // 2. 批量更新中心审核状态
            int updatedCount = roadSectionDistanceTestService.updateReview3ToZero();
            
            System.out.println("中心批量审核完成，更新记录数：" + updatedCount);
            return success("中心批量审核成功，共更新" + updatedCount + "条记录");
        } catch (Exception e) {
            System.err.println("中心批量审核失败：" + e.getMessage());
            return error("中心批量审核失败：" + e.getMessage());
        }
    }

    /**
     * 同步到正式表
     */
    @PreAuthorize("@ss.hasPermi('functiontest:RoadSectionDistanceTest:syncToRoadSectionDistance')")
    @Log(title = "主线站点间里程实测数据测试", businessType = BusinessType.UPDATE)
    @PostMapping("/syncToRoadSectionDistance")
    public AjaxResult syncToRoadSectionDistance()
    {
        try {
            String result = roadSectionDistanceTestService.syncDataToRoadSectionDistance();
            return AjaxResult.success(result);
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
    
    /**
     * 构建树形结构
     */
    private List<RoadSectionDistanceTest> buildTree(List<RoadSectionDistanceTest> list) {
        // 构建父子关系映射
        Map<Long, RoadSectionDistanceTest> nodeMap = new HashMap<>();
        List<RoadSectionDistanceTest> rootNodes = new ArrayList<>();
        
        // 先将所有节点放入map中
        for (RoadSectionDistanceTest node : list) {
            nodeMap.put(node.getId(), node);
        }
        
        // 构建父子关系
        for (RoadSectionDistanceTest node : list) {
            Long parentId = node.getParentId();
            if (parentId != null && parentId != 0) {
                RoadSectionDistanceTest parent = nodeMap.get(parentId);
                if (parent != null) {
                    if (parent.getChildren() == null) {
                        parent.setChildren(new ArrayList<RoadSectionDistanceTest>());
                    }
                    @SuppressWarnings("unchecked")
                    List<RoadSectionDistanceTest> children = (List<RoadSectionDistanceTest>) parent.getChildren();
                    children.add(node);
                }
            } else {
                rootNodes.add(node);
            }
        }
        
        return rootNodes;
    }
    
    /**
     * 将树形结构按照层次顺序展平为列表
     */
    private void flattenTree(List<RoadSectionDistanceTest> treeList, List<RoadSectionDistanceTest> resultList) {
        for (RoadSectionDistanceTest node : treeList) {
            // 添加当前节点到结果列表
            resultList.add(node);
            
            // 递归处理子节点
            if (node.getChildren() != null && !node.getChildren().isEmpty()) {
                @SuppressWarnings("unchecked")
                List<RoadSectionDistanceTest> children = (List<RoadSectionDistanceTest>) node.getChildren();
                flattenTree(children, resultList);
            }
        }
    }

}
